<template>
  <div>
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      :default-index="gender"
    />
  </div>
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'
export default {
  name: 'GenderEdit',
  data () {
    return {
      columns: ['男', '女']
    }
  },
  props: {
    gender: {
      type: Number,
      default: 0
    }
  },
  methods: {
    // 点击确认时触发的事件
    async onConfirm (value, index) {
      try {
        await updateUserProfileAPI({
          gender: index
        })
        this.$toast.success('修改成功')

        // 修改成功后需要关闭页面并更新内容
        this.$emit('update:gender', index)
        this.$emit('close')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改失败')
      }
    }
  }
}
</script>

<style>

</style>
